<template>
  <el-card>
    <el-form inline class="form-inline">
      <el-form-item label="一级分类">
        <el-select v-model="categoryStore.c1Id" @change="handler">
          <el-option
            v-for="(item, index) in categoryStore.c1Arr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select v-model="categoryStore.c2Id" @change="handler1">
          <el-option
            v-for="(item, index) in categoryStore.c2Arr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select v-model="categoryStore.c3Id">
          <el-option
            v-for="(item, index) in categoryStore.c3Arr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
import useCategoryStore from '@/store/modules/category'

let categoryStore = useCategoryStore()

//组件加载完成执行
onMounted(() => {
  categoryStore.getC1()
})

//监听
// watch(
//   () => categoryStore.c1Id,
//   () => {
//     categoryStore.getC2()
//   },
// )
//当下拉选项改变的时候触发
let handler = () => {
  //初始化二级，三级数据
  categoryStore.c2Id = ''
  categoryStore.c2Arr = []
  categoryStore.c3Id = ''
  categoryStore.c3Arr = []
  categoryStore.getC2()
}
let handler1 = () => {
  categoryStore.c3Id = ''
  categoryStore.c3Arr = []
  categoryStore.getC3()
}
</script>
<style lang="scss" scoped>
.form-inline {
  ::v-deep(.el-select) {
    width: 200px; // 设置为你想要的宽度
  }
}
</style>
